package com.int_tech.timeTask194;

import com.int_tech.MessageCodeAll.dao.CodeA2Dao;
import com.int_tech.MessageCodeAll.entity.CodeA2;
import com.int_tech.car.dao.CarDao;
import com.int_tech.car.entity.Car;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.DateTool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/22.
 */
@Component
public class CheckA2TimeTask {
    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private CodeA2Dao codeA2Dao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private StationDao stationDao;

    @Scheduled(cron="0/10 * * * * ?")
    public void checkA2() throws ParseException {

//        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByCodeAndCheckStatus("A2",8080);

        PageRequest pageRequest = new PageRequest(0, 5000);
//        Page<SocketLogSend> socketLogSendListPage = socketLogSendDao.findByCodeAndCheckStatusAndCarNumber(pageRequest,"A2",8080,"010AEA056");
        Date date = DateTool.string2Date("2018-08-03 00:00:00","yyyy-MM-dd HH:mm:ss");
        Page<SocketLogSend> socketLogSendListPage = socketLogSendDao.findByCodeAndCheckStatusAndCreateTimeAfterOrderByCreateTimeAsc(pageRequest,"A2",0,date);
        List<SocketLogSend> socketLogSendList = socketLogSendListPage.getContent();

        for(SocketLogSend socketLogSend : socketLogSendList)
        {
            try
            {
                socketLogSend.setCheckStatus(1);
                socketLogSendDao.save(socketLogSend);

                String logData = socketLogSend.getDataString();

                String[] logs = logData.split(",");

                //获取硬件时间
                String yjTime = logs[9];

                //创建时间
                String year = yjTime.substring(0,4);
                String month = yjTime.substring(4,6);
                String day = yjTime.substring(6,8);
                String hour = yjTime.substring(8,10);
                String min = yjTime.substring(10,12);
                String second = yjTime.substring(12,14);

                String dt = year+"-"+month+"-"+day+" "+hour+":"+min+":"+second;

                Date cd = DateTool.string2Date(dt,"yyyy-MM-dd HH:mm:ss");


                Calendar cal = Calendar.getInstance();
                cal.setTime(cd);//date 换成已经已知的Date对象
                cal.add(Calendar.HOUR_OF_DAY, 8);// before 8 hour
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(format.format(cal.getTime()));

                cd = DateTool.string2Date(format.format(cal.getTime()),"yyyy-MM-dd HH:mm:ss");


                CodeA2 codeA2 = new CodeA2();

                codeA2.setCarNumber(logs[2]);
                codeA2.setDataString(socketLogSend.getDataString());
                codeA2.setLng(logs[7]);
                codeA2.setLat(logs[8]);
                codeA2.setOpenStatus(Integer.parseInt(logs[10]));
                codeA2.setCreatedTime(cd);
                codeA2.setStatus(0);
                codeA2.setCheckStatus(0);

                codeA2Dao.save(codeA2);

                try
                {
                    //查询车辆相关联的管理员所属的场站信息
                    Car car = carDao.findCarByCarNumberAndIsDel(codeA2.getCarNumber(),0);
                    //车辆所属客户
                    int clientId = car.getClientId();
                    List<Station> stationList = stationDao.findByClientId(clientId);


                    //经纬度
                    String slng = codeA2.getLng();
                    String slat = codeA2.getLat();

                    Boolean isInStaion = false;

                    //所在场站
                    Station stationStartIn = new Station();

                    for (Station station:stationList)
                    {
                        try
                        {
                            String partitionLocation = "";

                            //场站坐标数组
                            String[] points = station.getArea().split("&");

                            int m1 =0;
                            for (String point:points)
                            {
                                try
                                {
                                    if(m1 == 0)
                                    {
                                        partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                                    }
                                    else
                                    {
                                        partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                                    }

                                    m1++;
                                }
                                catch (Exception e)
                                {
                                    e.printStackTrace();
                                }
                            }

                            String[] parts = partitionLocation.split("_");

                            if(parts.length > 3)
                            {
                                //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                                isInStaion = OrderMapTest.isInPolygon(slat,slng,partitionLocation);
                            }
                            else
                            {
                                //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
//                            System.out.print("要处理的位置信息为"+partitionLocation);
                                isInStaion = OrderMapTest.isInPolygonSingle(slat,slng,partitionLocation);
                            }

                            if(isInStaion)
                            {
                                stationStartIn = station;
                                codeA2.setIsInStation("1");
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                            codeA2.setStatus(2);
                        }

                    }
                    if(isInStaion)
                    {
                        codeA2.setLocation(stationStartIn.getName());
                        codeA2.setIsInStation("1");
                    }
                    else
                    {
                        codeA2.setLocation("场站外"+"("+slat+","+slng+")");
                        codeA2.setIsInStation("-1");
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    codeA2.setStatus(2);
                }

                codeA2Dao.save(codeA2);

                if(StringUtils.isEmpty(codeA2.getLocation()))
                {
                    String slng = codeA2.getLng();
                    String slat = codeA2.getLat();
                    codeA2.setLocation("场站外"+"("+slat+","+slng+")");
                    codeA2.setIsInStation("-1");
                    codeA2Dao.save(codeA2);
                }

            }
            catch (Exception e){

                e.printStackTrace();

                socketLogSend.setCheckStatus(2);

                socketLogSendDao.save(socketLogSend);
            }
        }
    }


    /**
     * 获取当天开关门坐标点的位置
     * @throws ParseException
     */
//    @Scheduled(cron="0/10 * * * * ?")
    public void getThisA2Location() throws ParseException {

        List<CodeA2> codeA2List = codeA2Dao.findByStatus(0);

//        List<Car> carList = carDao.findCarByIsDel(0);

        for (CodeA2 codeA2 : codeA2List) {

            codeA2.setStatus(1);

            try
            {
                //查询车辆相关联的管理员所属的场站信息
                Car car = carDao.findCarByCarNumberAndIsDel(codeA2.getCarNumber(),0);
                //车辆所属客户
                int clientId = car.getClientId();
                List<Station> stationList = stationDao.findByClientId(clientId);


                //经纬度
                String slng = codeA2.getLng();
                String slat = codeA2.getLat();

                Boolean isInStaion = false;

                //所在场站
                Station stationStartIn = new Station();

                for (Station station:stationList)
                {
                    try
                    {
                        String partitionLocation = "";

                        //场站坐标数组
                        String[] points = station.getArea().split("&");

                        int m1 =0;
                        for (String point:points)
                        {
                            try
                            {
                                if(m1 == 0)
                                {
                                    partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                                }
                                else
                                {
                                    partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                                }

                                m1++;
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        }

                        String[] parts = partitionLocation.split("_");

                        if(parts.length > 3)
                        {
                            //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                            isInStaion = OrderMapTest.isInPolygon(slat,slng,partitionLocation);
                        }
                        else
                        {
                            //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
//                            System.out.print("要处理的位置信息为"+partitionLocation);
                            isInStaion = OrderMapTest.isInPolygonSingle(slat,slng,partitionLocation);
                        }

                        if(isInStaion)
                        {
                            stationStartIn = station;
                            codeA2.setIsInStation("1");
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        codeA2.setStatus(2);
                    }

                }
                if(isInStaion)
                {
                    codeA2.setLocation(stationStartIn.getName());
                    codeA2.setIsInStation("1");
                }
                else
                {
                    codeA2.setLocation("场站外"+"("+slat+","+slng+")");
                    codeA2.setIsInStation("-1");
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                codeA2.setStatus(2);
            }

            codeA2Dao.save(codeA2);
        }
    }
}
